﻿using System;
using System.Text;
using NLog;
using System.Diagnostics;

namespace Libs.Utils
{
    public static class NLogLogger
    {
        static NLogLogger()
        {
            Logger = LogManager.GetCurrentClassLogger();
            
        }

        public static Logger Logger { get; set; }

        public static void LogInfo2(string message)
        {
            var mes = message + "\t" + DateTime.Now + Environment.NewLine;
            Logger.Info(mes);
        }

        public static void LogInfo(string message)
        {
            Info(message, false);
        }

        public static void Info(string message, bool sendMail)
        {
            // Debug mode thi ghi log, con Release thi o che khong ghi log
            //if (System.Configuration.ConfigurationSettings.AppSettings["Debug_Mode"].ToUpper() == "DEBUG")
            //{}
            var mes = GetCalleeString() + Environment.NewLine + "\t" + message;
            Logger.Info(":\t" + mes);
            if (sendMail)
                SendmailHelper.SendDebug(mes);

        }

        public static void Info(string message)
        {
            Info(message, false);
        }


        public static void TraceMessage(string message)
        {
            Logger.Trace("\t" + message);
        }
        public static void PublishException(Exception ex, bool sendmail)
        {
            DebugMessage(ex.Message + Environment.NewLine + ex.StackTrace, sendmail);

        }
        public static void PublishException(Exception ex)
        {
            PublishException(ex, true);
        }

        public static void DebugMessage(object o)
        {
            DebugMessage(GetValueOfObject(o));
        }

        public static void DebugMessage(string message, bool sendEmail)
        {
            var m = GetCalleeString() + Environment.NewLine + "\t" + message;
            Logger.Debug(":\t" + m);

            if (sendEmail)
                SendmailHelper.SendDebug(GetCalleeString() + Environment.NewLine + "\t" + message);

        }

        public static void DebugMessage(string message)
        {
            DebugMessage(message, false);
        }

        public static void LogDebug(string p)
        {
            DebugMessage(p);
        }

        public static void LogWarning(object o)
        {
            LogWarning(GetValueOfObject(o));
        }

        public static void LogWarning(string message, bool sendMail)
        {
            var error = GetCalleeString() + Environment.NewLine + "\t" + message;

            Logger.Warn(":\t" + error);

            if (sendMail)
                SendmailHelper.SendError(error);
        }

        public static void LogWarning(string message)
        {
            LogWarning(message, true);
        }

        public static void Fatal(string message)
        {
            Logger.Fatal(":\t" + GetCalleeString() + Environment.NewLine + "\t" + message);
        }

        private static string GetCalleeString()
        {
            var stackFrames = new StackTrace().GetFrames();
            if (stackFrames != null)
                foreach (var sf in stackFrames)
                {
                    if (sf.GetMethod().ReflectedType.Namespace != "Utils")
                    {
                        return string.Format("{0}.{1} ", sf.GetMethod().ReflectedType.Name, sf.GetMethod().Name);
                    }
                }

            return string.Empty;
        }


        public static string GetValueOfObject(object ob)
        {
            var sb = new StringBuilder();
            try
            {
                foreach (System.Reflection.PropertyInfo piOrig in ob.GetType().GetProperties())
                {
                    object editedVal = ob.GetType().GetProperty(piOrig.Name).GetValue(ob, null);
                    sb.AppendFormat("{0}:{1}\t ", piOrig.Name, editedVal);

                }
            }
            catch
            { }
            return sb.ToString();
        }
    }

    public class SendMail
    {
        public int MailID { get; set; }

        public string MailFromName { get; set; }

        public string MailFrom { get; set; }

        public string MailTo { get; set; }

        public string MailSubject { get; set; }

        public string MailBody { get; set; }

        public int Status { get; set; }

        public int ErrorCode { get; set; }
    }

    public class SendmailHelper
    {
        /// <summary>
        /// Send mail by smtp
        /// format by nguyen van khuyen
        /// date : 22/01/2010
        /// </summary>
        private const string VtcDomain = "smtp.mail.vtc.vn";
        private const string VtcUser = "support.vcoin";
        private const string VtcPassword = "khongcopass";
        static string _projectName = "eBank.Event2012.PetRacing";

        private static void Send(SendMail mailInfo)
        {
            //SmtpClient smtpClient;
            //var message = new MailMessage();

            //try
            //{
            //    smtpClient = new SmtpClient(vtcDomain, 25)
            //    {
            //        UseDefaultCredentials = true,
            //        Credentials = new NetworkCredential(vtcUser, vtcPassword)
            //    };
            //    //smtpClient = new SmtpClient("64.71.158.201", 25);
            //    message.From = new MailAddress("support.vcoin@vtc.vn", string.Format("VTC DEBUG [{0}]", Environment.MachineName));
            //    message.IsBodyHtml = false;
            //    message.Subject = MailInfo.MailSubject + " from " + Environment.MachineName;
            //    message.Body = MailInfo.MailBody;
            //    message.To.Add(MailInfo.MailTo);
            //    smtpClient.Send(message);
            //}
            //catch (Exception ex)
            //{
            //    NLogLogger.PublishException(ex, false);

            //    smtpClient = new SmtpClient(vtcDomain, 25);
            //    message.From = new MailAddress("support.vcoin@vtc.vn", string.Format("Alter VTC Debug [{0}]", Environment.MachineName));
            //    message.IsBodyHtml = false;
            //    message.Subject = MailInfo.MailSubject + " from " + Environment.MachineName;
            //    message.Body = MailInfo.MailBody;
            //    message.To.Add(MailInfo.MailTo);
            //    smtpClient.Send(message);
            //}

        }


        /// <summary>
        /// gửi thông báo lỗi tới email được config khi chương trình gặp lỗi
        /// </summary>
        /// <param name="messenger"></param>
        public static void SendError(string messenger)
        {


        }

        /// <summary>
        /// gửi thông báo lỗi tới email được config khi chương trình gặp lỗi
        /// </summary>
        /// <param name="messenger"></param>
        public static void SendDebug(string messenger)
        {

        }

    }
}
